<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Ace 编辑器复制代码示例</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <style>
        #editorContainer {
            position: relative;
            margin: 20px;
            border: 1px solid #ccc;
            border-radius: 4px;
            overflow: hidden;
        }
        #editor {
            height: 200px;
            width: 100%;
        }
        .copy-button {
            position: absolute;
            top: 10px;
            right: 10px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            padding: 5px 10px;
            cursor: pointer;
            font-size: 14px;
        }
        .copy-button:hover {
            background-color: #45a049;
        }
        .copy-confirmation {
            position: absolute;
            top: 10px;
            right: 10px;
            background-color: #28a745;
            color: white;
            border: none;
            border-radius: 4px;
            padding: 5px 10px;
            font-size: 14px;
            display: none;
        }
    </style>
</head>
<body>

    <div id="editorContainer">
        <div id="editor">function hello() { console.log("Hello, World!"); }</div>
        <button class="copy-button" id="copyButton"><i class="fas fa-copy"></i> 复制代码</button>
        <div class="copy-confirmation" id="copyConfirmation"><i class="fas fa-check"></i> 已复制！</div>
    </div>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.14/ace.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.14/theme-monokai.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.14/mode-javascript.js"></script>
    <script>
        // 初始化 Ace 编辑器
        const editor = ace.edit("editor");
        editor.setTheme("ace/theme/monokai");
        editor.session.setMode("ace/mode/javascript");

        // 获取按钮和确认提示的元素
        const copyButton = document.getElementById('copyButton');
        const copyConfirmation = document.getElementById('copyConfirmation');

        // 复制按钮点击事件
        copyButton.addEventListener('click', function() {
            // 获取编辑器中的代码
            const code = editor.getValue();

            // 创建一个临时的textarea元素来存放代码
            const tempTextarea = document.createElement('textarea');
            tempTextarea.value = code;
            document.body.appendChild(tempTextarea);

            // 选中并复制代码
            tempTextarea.select();
            document.execCommand('copy');

            // 移除临时的textarea元素
            document.body.removeChild(tempTextarea);

            // 显示“已复制”提示
            copyConfirmation.style.display = 'block';

            // 1秒后隐藏“已复制”提示
            setTimeout(() => {
                copyConfirmation.style.display = 'none';
            }, 1000);
        });
    </script>

</body>
</html>
